package com.lcp.se.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lcp.se.entity.data.Record;
import com.lcp.se.entity.result.PageResultForMini;
import com.lcp.se.entity.result.Result;
import com.lcp.se.mapper.RecordMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 查询求助/帮助记录
 *
 * @author lcp
 * @date 2020/8/17 22:26
 */
@Service
public class RecordService {
    @Resource
    private RecordMapper recordMapper;

    public Result<PageResultForMini<Record>> list(Integer pageSize, Integer currentPage,
                                                  Integer userType, String openId) {
        pageSize = pageSize == null ? 1 : pageSize;
        currentPage = currentPage == null ? 1 : currentPage;
        int index = pageSize * (currentPage - 1);
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        if (userType == 0) {
            queryWrapper.eq("from_user_open_id", openId);
        } else if (userType == 1) {
            queryWrapper.eq("handle_user_open_id", openId);
        }
        Integer count = recordMapper.selectCount(queryWrapper);
        queryWrapper.orderByDesc("create_time").last("limit " + index + "," + pageSize);
        List<Record> records = recordMapper.selectList(queryWrapper);
        PageResultForMini<Record> pageResult = new PageResultForMini<>(records, pageSize, currentPage, count);
        return Result.success("查询成功", pageResult);
    }

}
